<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/4
 * Time: 14:32
 */
namespace Home\Controller;

use Admin\Model\DoctorModel;
use Base\Controller\BaseHomeController;
use Think\Controller;
use Library\UploadFile;
use Think\Verify;

class DoctorController extends BaseHomeController
{
    protected $model;
    protected $upload;
    public function __construct()
    {
        parent::__construct(false,true,array(
            'input_name' => 'certification',//文本框name
            'save_path' => 'head',//保存路径
            'data_field' => 'certification'//数据库字段
        ));
        $this->model = new DoctorModel();

    }


//    public function index(){
//        $this->redirect('Control_index');
//    }
//用户注册
    public  function reg()
    {
        if (!empty($_POST)) {
            $data = $this->model->create();
            if (!$data) {
                $this->error($this->model->getError());
            } else {
                if ($this->upload) {
                    $input_name = 'certification';
                    $save_path = $this->uploadConfig['save_path'];
                    $data_field = $this->uploadConfig['data_field'];
                    if ($_FILES[$input_name]) {
                        $up = new UploadFile();
                        $info = $up->upFile($save_path, array('jpg', 'png', 'jpeg', 'gif'));
                        $data[$data_field] = $info[$input_name]['savepath'] . $info[$input_name]['savename'];
                    }
//                    $str = '123456789abcdefghijlmlipqrstunwxyzABCEJFJEWFJEIFJFJEWI';
//                    $code = '';
//                    for ($i = 0; $i < 6; $i++) {
//                        $code .= $str[mt_rand(0, strlen($str) - 1)];
//                    }
//                    $data['name'] = strtolower($code);
                    $add = $this->model->add($data);
                    if ($add) {
                        $this->model->save(array('id'=>$add,'name'=>"ysd$add"));
                        $this->success('注册成功',U('Index/index'));
                    }
                }
            }
        }
    }

    //用户登陆
    public function login()
    {
        $where['name'] = $_POST['name'];
        $where['phone'] = $_POST['name'];
        $where['email'] = $_POST['name'];
        $where['_logic'] = 'or';
        $map['_complex'] = $where;
        $map['pwd'] = md5($_POST['pwd']);
        $map['status'] = 1;
//        $data = $this->model->create($where);
        if (!$this->checkVerify(I('post.code'))) {
            $this->error('验证码不正确');
        }
        else {
            $result = $this->model->where($map)->find();
            if (!empty($result)){
                $token = md5($result['id'] . time());
                $this->model->where('status=1 and id=%d', array($result['id']))->setField('token', $token);
                $this->success('登陆成功',U('Index/index'));
            }else{
                $this->error('用户名或者密码错误');
            }

        }
    }

    /**
     * 验证码
     */
    public function verify()
    {
        $config = array(
            'imageW' => '150',
            'imageH' => '40',
            'fontSize' => 20,
            'length' => 4,
            'useNoise' => false,
            'useCurve' => false,
            'codeSet' => '0123456789',
        );
        ob_clean();
        $verify = new Verify($config);
        $verify->entry();
    }

    /**
     * 验证码是否正确
     * @param $code
     * @param string $id
     * @return bool
     */
    function checkVerify($code, $id = '')
    {
        $verify = new Verify();
        return $verify->check($code, $id);
    }
    //跳转忘记密码
    function  showPwd(){
        $this->display('passe');
    }
    /*
 * 找回密码
 *
 *
 */

    //获取手机验证码重置密码
    public function forgetPwd(){
        $map['phone'] = $_POST['phone'];
        $data = $this->model->where($map)->find();
        if (empty($data)){
            $this->Error('无此手机号');
        }else{
            $data['pwd'] = md5($_POST['pwd']);
            $result = $this->model->save($data);
            if ($result){
                $this->Success($result,'密码重置成功');
                $this->redirect(U('Index/index'));
            }else{
                $this->Error('密码重置失败');
            }
        }
    }
    //重置密码
    public function changePwd(){
        $where['name']  = $_POST['name'];
        $where['phone']  = $_POST['name'];
        $where['email']  = $_POST['name'];
        $where['_logic'] = 'or';
        $map['_complex'] = $where;
        $map['status']  = 1;
        $oldPwd = $_POST['pwd'];
        $newPwd = $_POST['pwd2'];
        if(!empty($oldPwd) || !empty($newPwd)){
                if($oldPwd == $newPwd){
                    $data = $this->model->field('id')->where($map)->find();
                    if ($data){
                        $result = $this->model->save(array('id'=>$data['id'],'pwd'=>md5($_POST['pwd2'])));
                        dump($result);
                        if($result){
                            $this->success($result,'重置密码成功');
                            $this->redirect('Index/index');
                        }else{
                            $this->Error('重置密码失败');
                        }
                    }else{
                        $this->Error('原密码错误');
                    }
            }
        }else{
            $this->Error('重置密码失败');
        }

    }
}